summaryrefslogtreecommitdiffstats
path: root/src/tests/common/scratch_buffer.cpp
blob: 26e4017603136b0c0a0d321751f9e295dc92bd5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <algorithm>
#include <array>
#include <cstring>
#include <span>
#include <catch2/catch_test_macros.hpp>
#include "common/common_types.h"
#include "common/scratch_buffer.h"

namespace Common {

TEST_CASE("ScratchBuffer: Basic Test", "[common]") {
    ScratchBuffer<u8> buf;

    REQUIRE(buf.size() == 0U);
    REQUIRE(buf.capacity() == 0U);

    std::array<u8, 10> payload;
    payload.fill(66);

    buf.resize(payload.size());
    REQUIRE(buf.size() == payload.size());
    REQUIRE(buf.capacity() == payload.size());

    std::memcpy(buf.data(), payload.data(), payload.size());
    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }
}

TEST_CASE("ScratchBuffer: resize_destructive Grow", "[common]") {
    std::array<u8, 10> payload;
    payload.fill(66);

    ScratchBuffer<u8> buf(payload.size());
    REQUIRE(buf.size() == payload.size());
    REQUIRE(buf.capacity() == payload.size());

    // Increasing the size should reallocate the buffer
    buf.resize_destructive(payload.size() * 2);
    REQUIRE(buf.size() == payload.size() * 2);
    REQUIRE(buf.capacity() == payload.size() * 2);

    // Since the buffer is not value initialized, reading its data will be garbage
}

TEST_CASE("ScratchBuffer: resize_destructive Shrink", "[common]") {
    std::array<u8, 10> payload;
    payload.fill(66);

    ScratchBuffer<u8> buf(payload.size());
    REQUIRE(buf.size() == payload.size());
    REQUIRE(buf.capacity() == payload.size());

    std::memcpy(buf.data(), payload.data(), payload.size());
    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }

    // Decreasing the size should not cause a buffer reallocation
    // This can be tested by ensuring the buffer capacity and data has not changed,
    buf.resize_destructive(1U);
    REQUIRE(buf.size() == 1U);
    REQUIRE(buf.capacity() == payload.size());

    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }
}

TEST_CASE("ScratchBuffer: resize Grow u8", "[common]") {
    std::array<u8, 10> payload;
    payload.fill(66);

    ScratchBuffer<u8> buf(payload.size());
    REQUIRE(buf.size() == payload.size());
    REQUIRE(buf.capacity() == payload.size());

    std::memcpy(buf.data(), payload.data(), payload.size());
    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }

    // Increasing the size should reallocate the buffer
    buf.resize(payload.size() * 2);
    REQUIRE(buf.size() == payload.size() * 2);
    REQUIRE(buf.capacity() == payload.size() * 2);

    // resize() keeps the previous data intact
    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }
}

TEST_CASE("ScratchBuffer: resize Grow u64", "[common]") {
    std::array<u64, 10> payload;
    payload.fill(6666);

    ScratchBuffer<u64> buf(payload.size());
    REQUIRE(buf.size() == payload.size());
    REQUIRE(buf.capacity() == payload.size());

    std::memcpy(buf.data(), payload.data(), payload.size() * sizeof(u64));
    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }

    // Increasing the size should reallocate the buffer
    buf.resize(payload.size() * 2);
    REQUIRE(buf.size() == payload.size() * 2);
    REQUIRE(buf.capacity() == payload.size() * 2);

    // resize() keeps the previous data intact
    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }
}

TEST_CASE("ScratchBuffer: resize Shrink", "[common]") {
    std::array<u8, 10> payload;
    payload.fill(66);

    ScratchBuffer<u8> buf(payload.size());
    REQUIRE(buf.size() == payload.size());
    REQUIRE(buf.capacity() == payload.size());

    std::memcpy(buf.data(), payload.data(), payload.size());
    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }

    // Decreasing the size should not cause a buffer reallocation
    // This can be tested by ensuring the buffer capacity and data has not changed,
    buf.resize(1U);
    REQUIRE(buf.size() == 1U);
    REQUIRE(buf.capacity() == payload.size());

    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }
}

TEST_CASE("ScratchBuffer: Span Size", "[common]") {
    std::array<u8, 10> payload;
    payload.fill(66);

    ScratchBuffer<u8> buf(payload.size());
    REQUIRE(buf.size() == payload.size());
    REQUIRE(buf.capacity() == payload.size());

    std::memcpy(buf.data(), payload.data(), payload.size());
    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }

    buf.resize(3U);
    REQUIRE(buf.size() == 3U);
    REQUIRE(buf.capacity() == payload.size());

    const auto buf_span = std::span<u8>(buf);
    // The span size is the last requested size of the buffer, not its capacity
    REQUIRE(buf_span.size() == buf.size());

    for (size_t i = 0; i < buf_span.size(); ++i) {
        REQUIRE(buf_span[i] == buf[i]);
        REQUIRE(buf_span[i] == payload[i]);
    }
}

TEST_CASE("ScratchBuffer: Span Writes", "[common]") {
    std::array<u8, 10> payload;
    payload.fill(66);

    ScratchBuffer<u8> buf(payload.size());
    REQUIRE(buf.size() == payload.size());
    REQUIRE(buf.capacity() == payload.size());

    std::memcpy(buf.data(), payload.data(), payload.size());
    for (size_t i = 0; i < payload.size(); ++i) {
        REQUIRE(buf[i] == payload[i]);
    }

    buf.resize(3U);
    REQUIRE(buf.size() == 3U);
    REQUIRE(buf.capacity() == payload.size());

    const auto buf_span = std::span<u8>(buf);
    REQUIRE(buf_span.size() == buf.size());

    for (size_t i = 0; i < buf_span.size(); ++i) {
        const auto new_value = static_cast<u8>(i + 1U);
        // Writes to a span of the scratch buffer will propagate to the buffer itself
        buf_span[i] = new_value;
        REQUIRE(buf[i] == new_value);
    }
}

} // namespace Common